package com.zhao.controller.show;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.comm.log.MyLog;
import com.zhao.pojo.*;
import com.zhao.service.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

@Controller
@RequestMapping("/userCenter")
public class UserCenterController {

    @Autowired
    UserService userService;
    @Autowired
    ArticleService articleService;
    @Autowired
    WalletService walletService;
    @Autowired
    FollowService followService;
    @Autowired
    DatafileService datafileService;
    @Autowired
    ChartService chartService;

    @RequestMapping("/show/{id}")
    public String UserCenter(@PathVariable String id,
                             @RequestParam(value = "words",required = false)String like,
                             @RequestParam(value = "searchType",required = false,defaultValue = "boke")String searchType,
                             Model model){
        System.out.println("============>"+id);
        boolean result=id.matches("[0-9]+");
        User user = null;
        if(result){
            user = userService.getById(id);
        }else {
            user = userService.getOne(new QueryWrapper<User>().eq("user_account",id));
            id = user.getUserId().toString();
        }


        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        Follow one=null;
        if (loginUser!=null){
            one = followService.getOne(new QueryWrapper<Follow>().eq("uid", loginUser.getUserId()).eq("fid", id));
        }
        if (user==null){
            return "redirect:/show/main";
        }
        //判断当前查询id下的全部  文章信息 资源  收藏
        if (searchType.equals("ziyuan")){
            QueryWrapper<Datafile> wrapper = new QueryWrapper<Datafile>();
            wrapper.eq("author", user.getUserAccount()).orderByDesc("create_time");
            if (like!=null) {
                wrapper.like("title", like);
            }
            List<Datafile> datafiles = datafileService.list(wrapper);
            model.addAttribute("datafiles",datafiles);
        }else if (searchType.equals("shoucang")){
            List<String> titles = chartService.CollectgroupAll(user.getUserAccount());
            if (titles.size()>0){
                Map<String,List<Chart>> map=new HashMap<>();
                for (String title:titles) {
                    List<Chart> charts = chartService.CollectByAll(user.getUserAccount(), title);
                    map.put(title,charts);
                }
                model.addAttribute("collects",map);
            }else {
                model.addAttribute("collects",null);
            }

        }else {
            List<Article> articles = articleService.selectListTop(like, user.getUserAccount());
            model.addAttribute("articles",articles);
        }
        model.addAttribute("searchType",searchType);
        //总排名
        String rank= articleService.listTop(user.getUserAccount());
        if ("".equals(rank)||rank==null){
            rank="无作品";
        }else if (Integer.parseInt(rank)>100){
            rank="未上榜";
        }
        //id的钱包
        Wallet wallet = walletService.getOne(new QueryWrapper<Wallet>().eq("uid", id));
        //总评论 赞 浏览量
        Statistic statistic =userService.SumStatistic(user.getUserAccount());
        if (statistic==null){
            statistic =new Statistic();
            statistic.setHits(0);
            statistic.setCommentsNum(0);
            statistic.setZan(0);
        }
        if (Objects.isNull(statistic.getHits())){
            statistic.setHits(0);
        }
        if (Objects.isNull(statistic.getCommentsNum())){
            statistic.setCommentsNum(0);
        }
        if (Objects.isNull(statistic.getZan())){
            statistic.setZan(0);
        }
        //最新评论
        List<Comment> comments= userService.ListNewcomment(user.getUserAccount());
        //热门文章5
        List<Article> hots=userService.ListhotsArticle(5,user.getUserAccount());
        //粉丝数
        List<Follow> follows = followService.list(new QueryWrapper<Follow>().eq("fid", id));
        if (follows==null){
            model.addAttribute("follow","0");
        }else {
            model.addAttribute("follow",follows.size());
        }
        //判断 是否关注了
        if (one==null){
            model.addAttribute("iffollow","no");
        }else {
            model.addAttribute("iffollow","yes");
        }
        //是否有查询 有返回查询条件
        if (like!=null){
            model.addAttribute("like",like);
        }

        model.addAttribute("comments",comments);

        model.addAttribute("rank",rank);

        model.addAttribute("statistic",statistic);

        model.addAttribute("wallet",wallet);

        model.addAttribute("user",user);

        model.addAttribute("hots",hots);
        return "show/userCenter";
    }

    @RequestMapping("/follow/{fid}/{iffollow}")
    public String follow(@PathVariable String fid,@PathVariable String iffollow){
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        userService.saveorUpdateFollow(loginUser.getUserId(),Integer.parseInt(fid),iffollow);
       // userService.addFollow(loginUser.getUserId(),id);
        return "redirect:/userCenter/show/"+fid;
    }
}
